package com.itbaizhan.hbase2hdfs;

import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class Hbase2HdfsMapper extends TableMapper<Text, IntWritable> {
    //定义输出的key value对象
    private Text keyOut = new Text();
    private IntWritable valOut = new IntWritable(1);
    @Override
    protected void map(ImmutableBytesWritable key, Result value, Mapper<ImmutableBytesWritable, Result, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        //key:对应的就是HBase表当前行数据的rowkey
        System.out.println("key:"+key.toString());
        //value就是从hbase读取到的一行数据的Result对象
        //读取当前行数据中的cf:line单元格中的数据
        byte[] data = value.getValue("cf".getBytes(), "line".getBytes());
        //将数据转换为字符串
        String line = Bytes.toString(data);
        //按照空格拆分
        String[] words = line.split(" ");
        //遍历输出
        for(String word:words){
            keyOut.set(word);
            context.write(keyOut,valOut);
        }
    }
}
